package com.goldman.aplt.job;

import com.fasterxml.jackson.databind.JsonNode;
import com.goldman.aplt.base.constant.ApConstants;
import com.goldman.aplt.entity.parameter.AppSystemParameter;
import com.goldman.aplt.service.parameter.AppSystemParameterService;
import com.goldman.aplt.util.CommUtil;
import com.goldman.http.common.s2s.S2SHttp;
import com.goldman.http.common.s2s.S2SResponse;
import com.goldman.http.common.s2s.util.JsonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AfterEodJob {

    @Value("${xxl.job.executor.addresses:null}")
    private String address;
    @Autowired
    private AppSystemParameterService appSystemParameterService;

    @XxlJob(value = "afterEodJob")
    public ReturnT<String> execute(String customParams) {
        try {
            XxlJobLogger.log("XXL-JOB, afterEodJob Start");

            List<AppSystemParameter> jobIds = appSystemParameterService.lambdaQuery().eq(AppSystemParameter::getParmCode, ApConstants.AFTER_EOD_JOB_KEY).list();
            if (address.equals("null") || jobIds == null || jobIds.isEmpty()) {
                log.info("afterEodJob is empty!");
                XxlJobLogger.log("XXL-JOB, afterEodJob End");
                return ReturnT.SUCCESS;
            }

            for (AppSystemParameter jobId : jobIds) {

                Map<String, Object> parms = new HashMap<>();
                parms.put("glueType", "BEAN");
                parms.put("executorHandler", jobId.getParmValue());
                parms.put("executorParams", customParams);
                parms.put("jobId", Integer.parseInt(jobId.getPrimaryKey()));
                S2SResponse<Object> response = S2SHttp.post(address + "/run", JsonUtil.toJsonStr(parms));
                if (!response.isHTTPSuccess() || !response.getResponseData().isSuccess()) {
                    log.error("afterEodJob push job error,jobid {}", jobId.getPrimaryKey());
                    continue;
                }
                JsonNode resp = JsonUtil.toTree(JsonUtil.toJsonStr(response.getResponseData().getData()));

                JsonNode statusNode = resp.get("status");
                if (CommUtil.isNull(statusNode) || statusNode.get("code").asInt() != 0) {
                    log.error("afterEodJob sub job excute failure, jobid {}", jobId.getPrimaryKey());
                }
            }

            log.info("afterEodJob execute success!, parmams {}", customParams);
            XxlJobLogger.log("XXL-JOB, afterEodJob End");
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("afterEodJob Exception", e);
            XxlJobLogger.log("XXL-JOB, afterEodJob End");
            return ReturnT.FAIL;
        }
    }
}
